package com.pll.redisInitialization;

import com.pll.constant.ConstantField;
import com.pll.mapper.CommodityMapper;
import com.pll.mapper.OrderMapper;
import com.pll.pojo.Goods;
import com.pll.pojo.GoodsType;
import com.pll.pojo.Order;
import com.pll.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: RedisInitialization
 * @author: XR_Z
 * @date: 2025/6/7 21:54
 * @Version: 1.0
 * @description: 初始化redis
 */
@Component
public class RedisInitialization implements CommandLineRunner {

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private CommodityMapper commodityMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void run(String... args) throws Exception {      //项目启动时执行
        //同步所有商品
        addAllProducts();

        //同步商品分类
        addAllProductCategories();
    }

    /**
     * 向redis中添加所有商品
     */
    protected void addAllProducts(){
        //查询所有商品
        List<Goods> itAll = commodityMapper.getItAll();

        //将商品存入到redis中
        for (Goods good : itAll) {
            commodityService.saveGoods(good);
        }

        System.out.println("商品成功添加到redis中~😮‍💨😮‍💨😮‍💨");
    }

    /**
     * 向redis中添加所有商品
     */
    protected void addAllProductCategories(){
        List<GoodsType> itAll = commodityMapper.goodsTypeAll();
        for (GoodsType good : itAll) {
            commodityService.saveGoodsType(good);
        }

        System.out.println("商品分类成功添加到redis中~😎😎😎");
    }

}
